package com.yunduansing.demo.mapper;

import com.yunduansing.demo.entity.UserRoleEntity;
import com.yunduansing.demo.model.user.UserRoleModel;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yunduansing
 * @since 2020-05-24
 */
@Mapper
public interface UserRoleMapper extends BaseMapper<UserRoleEntity> {
    @Insert("insert into sys_user_role(userId,roleId) values(@userId,@roleId)")
    boolean create(UserRoleEntity entity);

    int batchCreate(List<UserRoleEntity> entities);

    /**
     * 根据用户ID查询用户权限
     * @param userId 用户ID
     * @return 用户权限列表
     */
    @Select("select role_Id,user_id id,b.name roleName,b.code roleCode from sys_user_role a left join sys_role b on b.id=a.role_id where a" +
            ".user_id in" +
            "(#{userId})")
    List<UserRoleModel> findRolesByUser(long userId);

    @Delete("delete from sys_user_role where user_id=#{userId}")
    boolean deleteByUserId(int userId);
}
